﻿#!/bin/python3
# 
# 参考[1] 《Storing Battery Fuel Gauge Parameters in DS2780》
# https://www.maximintegrated.com/cn/app-notes/index.mvp/id/3584
# 给出的计算公式得到




# config_maxin.py 是参考[1]的参数，用于验证代码与DS2780K计算的是否一样
# import config_maxim as config

# config_default.py 是本项目电池的特性配置
import config_default as config  # 只要该文件的参数
# import config_default2 as config  # 只要该文件的参数



def format_code(reg, value):
    # print("reg 0x%0.2x value 0x%0.2x" %(reg, value))
    print("ds278x_write_byte(info, 0x%0.2x, 0x%0.2x);" %(reg, value));
    # print("sd,'", reg)
    pass

def clac_AB(AccBias_mA, SensoR_mohm):
    unit=1.5325  # uV
    AccBias_uV = AccBias_mA* SensoR_mohm 
    result=AccBias_uV
    stored= int(result / unit)
    format_code(0x61, stored)
    pass

def clac_AC(AgingCapacity_mAhrs, SensoR_mohm):
    unit=6.25  # uVhr
    AgingCapacity_uVhrs = AgingCapacity_mAhrs * SensoR_mohm 
    result = AgingCapacity_uVhrs
    stored= int(result / unit)

    format_code(0x62, stored >> 8)
    format_code(0x63, stored & 0xff)

def clac_VCHG(ChargeVoltage_mV):
    unit=19.52  # mV
    result = ChargeVoltage_mV
    stored= int(result / unit)
    format_code(0x64, stored)

def clac_IMIN(ChargeCurrent_mA, SensoR_mohm):
    unit=50  # uV
    ChargeCurrent_uV = ChargeCurrent_mA * SensoR_mohm 
    result = ChargeCurrent_uV
    stored= int(result / unit)
    format_code(0x65, stored)


def clac_VAE(AEVoltage_mV):
    unit=19.52  # mV
    result = AEVoltage_mV
    stored= int(result / unit)
    format_code(0x66, stored)
    
def clac_IAE(AECurrent_mA, SensoR_mohm):
    unit=200  # mV
    AECurrent_uV = AECurrent_mA * SensoR_mohm
    result = AECurrent_uV
    stored= int(result / unit)
    format_code(0x67, stored)


def clac_ActiveEmpty40(ActiveEmpty_40_mAhrs, Full_40_mAhrs):
    unit=Full_40_mAhrs * pow(2,-10)  # mV
    result = ActiveEmpty_40_mAhrs
    stored= int(result / unit)
    format_code(0x68, stored)

def clac_PSNSP(SensoR_mohm):
    unit=1  # mV
    k = 1 / (SensoR_mohm /1000)
    result = k
    stored= int(result / unit)
    format_code(0x69, stored)
# def calc_FULL401(FULL_40_mAhrs, SensoR_mohm):
    # pass
def calc_FULL40(FULL_40_mAhrs,SensoR_mohm):
    
    unit=6.25  # uVhr
    FULL40_mVhrs = FULL_40_mAhrs * SensoR_mohm
    result = FULL40_mVhrs
    stored= int(result / unit)
    format_code(0x6a, stored >> 8)
    format_code(0x6b, stored & 0xff)

def calc_FullSlopes(Full_40_mAhrs,Full_30_mAhrs,Full_20_mAhrs,Full_10_mAhrs,Full_00_mAhrs):
    unit= 10 * pow(2,-14)  # mV
    sf30t40=1 - Full_30_mAhrs/Full_40_mAhrs
    sf20t30=Full_30_mAhrs/Full_40_mAhrs - Full_20_mAhrs/Full_40_mAhrs
    sf10t20=Full_20_mAhrs/Full_40_mAhrs - Full_10_mAhrs/Full_40_mAhrs
    sf00t10 =Full_10_mAhrs/Full_40_mAhrs - Full_00_mAhrs/Full_40_mAhrs
    
    
    result = sf30t40
    stored= int(result / unit)
    format_code(0x6c, stored)
    result = sf20t30
    stored= int(result / unit)
    format_code(0x6d, stored)
    result = sf10t20
    stored= int(result / unit)
    format_code(0x6e, stored)
    result = sf00t10
    stored= int(result / unit)
    format_code(0x6f, stored)
    
def calc_AESlopes(FULL_40_mAhrs, AE_40_mAhrs,AE_30_mAhrs,AE_20_mAhrs,AE_10_mAhrs,AE_00_mAhrs):
    unit= 10 * pow(2,-14)  # mV
    sf30t40=AE_30_mAhrs/FULL_40_mAhrs - AE_40_mAhrs/FULL_40_mAhrs
    sf20t30=AE_20_mAhrs/FULL_40_mAhrs - AE_30_mAhrs/FULL_40_mAhrs
    sf10t20=AE_10_mAhrs/FULL_40_mAhrs - AE_20_mAhrs/FULL_40_mAhrs
    sf00t10=AE_00_mAhrs/FULL_40_mAhrs - AE_10_mAhrs/FULL_40_mAhrs
    
    
    result = sf30t40
    stored= int(result / unit)
    format_code(0x6c, stored)
    result = sf20t30
    stored= int(result / unit)
    format_code(0x6d, stored)
    result = sf10t20
    stored= int(result / unit)
    format_code(0x6e, stored)
    result = sf00t10
    stored= int(result / unit)
    format_code(0x6f, stored)

def calc_SESlopes(FULL_40_mAhrs, SE_40_mAhrs,SE_30_mAhrs,SE_20_mAhrs,SE_10_mAhrs,SE_00_mAhrs):
    unit= 10 * pow(2,-14)  # mV
    sf30t40=SE_30_mAhrs/FULL_40_mAhrs - SE_40_mAhrs/FULL_40_mAhrs
    sf20t30=SE_20_mAhrs/FULL_40_mAhrs - SE_30_mAhrs/FULL_40_mAhrs
    sf10t20=SE_10_mAhrs/FULL_40_mAhrs - SE_20_mAhrs/FULL_40_mAhrs
    sf00t10=SE_00_mAhrs/FULL_40_mAhrs - SE_10_mAhrs/FULL_40_mAhrs
    
    # print(sf30t40)
    # print(sf20t30)
    result = sf30t40
    stored= int(result / unit)
    format_code(0X74, stored)
    # format_code(0X74, 0)
    result = sf20t30
    stored= int(result / unit)
    format_code(0x75, stored)
    result = sf10t20
    stored= int(result / unit)
    format_code(0x76, stored)
    result = sf00t10
    stored= int(result / unit)
    format_code(0x77, stored)

def calc_RSGAIN(RSGAIN):
    stored=int(RSGAIN*1024)
    format_code(0x78, stored >> 8)
    format_code(0x79, stored & 0xff)
def calc_RSTC(RSTCO):
    unit=30.5176
    stored = int(RSTCO/unit)
    format_code(0x7a, stored)

if __name__ == '__main__':
    clac_AB(config.AccBias_mA, config.SensoR_mohm)
    clac_AC(config.AgingCapacity_mAhrs, config.SensoR_mohm)
    clac_VCHG(config.ChargeVoltage_mV)
    clac_IMIN(config.ChargeCurrent_mA, config.SensoR_mohm)
    clac_VAE(config.AEVoltage_mV)
    clac_IAE(config.AECurrent_mA, config.SensoR_mohm)
    clac_ActiveEmpty40(config.ActiveEmpty_40_mAhrs, config.Full_40_mAhrs)
    clac_PSNSP(config.SensoR_mohm)

    calc_FULL40(config.Full_40_mAhrs, config.SensoR_mohm)
    calc_FullSlopes(config.Full_40_mAhrs,config.Full_30_mAhrs,config.Full_20_mAhrs,config.Full_10_mAhrs,config.Full_00_mAhrs)
    calc_AESlopes(config.Full_40_mAhrs, config.AE_40_mAhrs,config.AE_30_mAhrs,config.AE_20_mAhrs,config.AE_10_mAhrs,config.AE_00_mAhrs)
    calc_SESlopes(config.Full_40_mAhrs, config.SE_40_mAhrs,config.SE_30_mAhrs,config.SE_20_mAhrs,config.SE_10_mAhrs,config.SE_00_mAhrs)
    calc_RSGAIN(config.RSGAIN)
    calc_RSTC(config.RSTCO)